/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.seu.cose.hobbysharing.dao;

import cn.edu.seu.cose.hobbysharing.bean.Hobby;
import cn.edu.seu.cose.hobbysharing.bean.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import cn.edu.seu.cose.hobbysharing.dao.*;
import java.sql.Statement;


/**
 *
 * @author Yan
 */
public class RelationDao {
    private ConnectionFactory f ;
    private User getUserByResultSet (ResultSet rs) throws SQLException{
        String user_uuid = rs.getString("user_uuid");
        String user_display_name = rs.getString("user_display_name");
        User user = new User();
        user.setUserUuid(user_uuid);
        user.setUserDisplayName(user_display_name);
        return user;
    }
    public Collection<User> getFriendsByUser (String user_uuid) throws SQLException{
        Connection c= f.getConnection() ;
        //Connection c = f.getConnection();
        try {
        PreparedStatement ps = c.prepareStatement("select * from hs_user_friend_relationship where user_uuid=? ");
        ps.setString(1, user_uuid);
        ResultSet rs = ps.executeQuery();
        Collection <User> users = new LinkedList<User>();
        while (rs.next()){
            users.add(getUserByResultSet(rs));
        }
        return users;
        }
        finally {
            c.close();
        }
    
    }
    public void addFriend (String user_uuid, String friend_uuid) throws SQLException{
        Connection c= f.getConnection() ;
        try {
        PreparedStatement ps = c.prepareStatement("insert into hs_user_friend_relationship(user_uuid,friend_uuid) values (?,?)",
        Statement.RETURN_GENERATED_KEYS);
        ps.setString(1, user_uuid);
        ps.setString(2,friend_uuid);
        ResultSet rs = ps.executeQuery();
        }
        finally {
            c.close();
        }
    }
    
    public void deleteFriend (String user_uuid, String friend_uuid)throws SQLException{
        Connection c= f.getConnection() ;
        try {
        PreparedStatement ps = c.prepareStatement("delete from hs_user_friend_relationship where (user_uuid,friend_uuid) values (?,?)");
        ps.setString(1, user_uuid);
        ps.setString(2, friend_uuid);
        ResultSet rs = ps.executeQuery();
        }
        finally {
            c.close();
        }
    }
   
}
